<template>
  <div class="app-container" style="'position: relative;">
    <div class="qrcode" style="position: absolute;right:80px;" ref="qrCodeUrl"></div>
    <div style="position: absolute;top:80px;">
      <el-row :gutter="10" class="mb8" >
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
            v-hasRole="['admin','conservator']"
            v-hasPermi="['participation:participation:add']"
          >新增</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="success"
            plain
            icon="el-icon-edit"
            size="mini"
            :disabled="single"
            @click="handleUpdate"
            v-hasRole="['admin','conservator']"
            v-hasPermi="['participation:participation:edit']"
          >修改</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="danger"
            plain
            icon="el-icon-delete"
            size="mini"
            :disabled="multiple"
            @click="handleDelete"
            v-hasRole="['admin','conservator']"
            v-hasPermi="['participation:participation:remove']"
          >删除</el-button>
        </el-col>
        <el-col :span="1.5">
          <el-button
            type="warning"
            plain
            icon="el-icon-download"
            size="mini"
            @click="handleExport"
            v-hasPermi="['participation:participation:export']"
          >导出</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>
    </div>

    <el-table style="margin-top: 115px;" v-loading="loading" :data="participationList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column  type="index" label="序号" width="100" align="center" :index="indexMethod" />
      <el-table-column label="标题" width="300"  prop="title" />
      <el-table-column label="姓名" align="center" prop="publicName" />
      <!-- ；1=男，2=女 -->
      <!-- <el-table-column label="性别" align="center" prop="sex" >
        <template slot-scope="scope">
          <span>{{scope.row.sex == 1 ?'男':scope.row.sex == 2?'女':''}}</span>
        </template>
      </el-table-column> -->
      <el-table-column label="属地" align="center" prop="area">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.ps_possession" :value="scope.row.area"/>
        </template>
      </el-table-column>
      <el-table-column label="详细地址" align="center" prop="address" />
      <!-- <el-table-column label="详情" align="center" prop="content" /> -->
      <el-table-column label="手机号" align="center" prop="photonumber" />
      <!-- <el-table-column label="ip地址" align="center" prop="ip" /> -->
      <!-- ；1=待处理；2=不采纳；3=已处理；4=删除 -->
      <el-table-column label="状态" align="center" prop="status" >
        <template slot-scope="scope">
          <span>{{scope.row.status == 1 ?'待处理':scope.row.status == 2?'不采纳':scope.row.status == 3?'已处理':''}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleDetails(scope.row)"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status == 1"
            @click="gotoPath(scope.row)"
            v-hasRole="['admin','conservator']"
            v-hasPermi="['participation:participation:edit']"
          >转处置</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.status != 2"
            @click="notCn(scope.row)"
            v-hasRole="['admin','conservator']"
            v-hasPermi="['participation:participation:remove']"
          >不采纳</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改公众参与对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="标题" prop="title">
          <el-input v-model="form.title" placeholder="请输入标题" />
        </el-form-item>
        <el-form-item label="姓名" prop="publicName">
          <el-input v-model="form.publicName" placeholder="请输入提交人姓名" />
        </el-form-item>
        <el-row :gutter="24" type="flex" justify="space-between">
          <el-col :span="11">
            <el-form-item label="手机号" prop="photonumber">
              <el-input v-model="form.photonumber" placeholder="请输入手机号" />
            </el-form-item>
          </el-col>
          <el-col :span="11">
            <el-form-item label="属地" prop="area">
              <el-select style="width: 100%;" v-model="form.area" placeholder="请选择舆情属地">
                <el-option
                  v-for="dict in dict.type.ps_possession"
                  :key="dict.value"
                  :label="dict.label"
                  :value="parseInt(dict.value)"
                ></el-option>
              </el-select>
            </el-form-item>
           </el-col>
          </el-row>
        <el-form-item label="详细地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="内容">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {notAdopt,listParticipation, getParticipation, delParticipation, addParticipation, updateParticipation,getEwmCode } from "@/api/participation/participation";
import QRCode from 'qrcodejs2'
export default {
  name: "Participation",
  dicts: ['ps_possession'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公众参与表格数据
      participationList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  mounted() {
    this.getEwmCode()
  },
  methods: {
    getEwmCode(){
      getEwmCode().then(res=>{
        var qrcode = new QRCode(this.$refs.qrCodeUrl, {
          text: res.url, // 需要转换为二维码的内容
          width: 100,
          height: 100,
          colorDark: '#000000',
          colorLight: '#ffffff',
          correctLevel: QRCode.CorrectLevel.H
        })
      })
    },
    indexMethod(index) {
       index = (index + 1) + (this.queryParams.pageNum - 1) * this.queryParams.pageSize
       return index
     },
    /** 查询公众参与列表 */
    getList() {
      this.loading = true;
      listParticipation(this.queryParams).then(response => {
        this.participationList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    //详情
    handleDetails(row){
      this.$router.push({path:'/participationParticulars',query: {id:row.id}})
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        publicName: null,
        sex: 0,
        area: null,
        address: null,
        content: null,
        photonumber: null,
        ip: null,
        status: 0
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加公众参与";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getParticipation(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改公众参与";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateParticipation(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addParticipation(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除公众参与编号为"' + ids + '"的数据项？').then(function() {
        return delParticipation(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('participation/participation/export', {
        ...this.queryParams
      }, `participation_${new Date().getTime()}.xlsx`)
    },
    gotoPath(row){
      this.form = row;
      delete this.form.id
      this.$router.push({path:'/publicsentiment',query: {form:JSON.stringify(this.form)}})
    },
    notCn(row){
      notAdopt({id:row.id}).then(res=>{
        this.getList();
      })
    }
  }
};
</script>
<style lang="scss" scoped>
  ::v-deep .top-right-btn{
    display: none;
  }
</style>
